(function(window, undefined) {
	var Sns = {};
	window.Sns = window.Sns || Sns;

	Sns.token_info = "";
	Sns.fb_graph_url = "https://graph.facebook.com";
	Sns.ajax_url = "http://buzzbee.kr/s.oh/guppii_ver1/guppiiphp_ver1/"; /*변경해야합니다.*/
	Sns.share_url = "http://buzzbee.kr/gpark/guppiiweb_ver1/store_view.php?id="; /*변경해야합니다.*/
	Sns.base_url = "http://buzzbee.kr/guppii/sns/";
	Sns.fb_url = Sns.base_url + "redirect/to_facebook.php";
	Sns.fb_url_up = Sns.base_url + "redirect/to_facebook_up.php";
	Sns.tw_url = Sns.base_url + "redirect/to_twitter.php";
	Sns.tw_url_up = Sns.base_url + "redirect/to_twitter_up.php";
	Sns.redirect_url = "http://buzzbee.kr/s.oh/guppii_ver1/guppiiweb_ver1/";
	
	Sns.eventListenner = function(){
		var that = this;
		$('#fbLoginBtn').off('click').on('click',function(event){
			event.preventDefault();
			that.loginByFacebook();
		});
	}
	
	Sns.loginByFacebook = function(doit){
		var that = this,
		url = that.fb_url;
		if( doit=='update' ){ 
			url = that.fb_url_up+ "?user_id=" + sessionStorage.getItem('user_id'); 
		}
		location.href = url;
	}
	
	Sns.loginByTwitter = function(doit){
		var that = this,
		url = that.tw_url;
		if( doit=='update' ){ 
			url = that.tw_url_up+ "?user_id=" + sessionStorage.getItem('user_id'); 
		}
		location.href = url;
	}
	
	Sns.loadToken = function(){
		var that = this;
		$.ajax({
			type : "post",
			url : that.ajax_url + "account.php?action=loadToken",
			dataType : "json",
			data : { "user_id" : sessionStorage.getItem('user_id') },
			success : function(result) { 
				that.loadTokenCallback(result);
			},
		});		
  	};
  	
  	Sns.isPhone = function( user_id ){		
		var that = this;
		$.ajax({
			type : "post",
			url : that.ajax_url + "account.php?action=checkRegistPhone",
			dataType : "json",
			data : { "user_id" : sessionStorage.getItem('user_id') },
			success : function(result) { 
				if(result=="true"){
					alert('인증이완료된사람입니다.');
				}else{
					alert('인증이필요합니다.');
				}
			},
		});		
  	};
  	
  	Sns.loadTokenCallback = function(result){
  		var that = this;
  		if( result.length > 0 ){
			var i = 0,
			exist = 0,
			length = result.length;
			
			for( i=0; i<length; i++ ){
				if(result[i].sns_type == "1"){	
					sessionStorage.setItem('fb_token',result[i].token);
				}
				else if(result[i].sns_type == "2"){
					sessionStorage.setItem('tw_token',result[i].token);
					sessionStorage.setItem('tw_token_secret',result[i].token);
				} 
			}
			/*폰 인증여부확인*/
			that.isPhone();
		}
  	};
  	
  	Sns.isFbToken = function(){
  		var that = this;
  		if( sessionStorage.getItem('fb_token') ){ return true; }
  		else{
  			that.loginByFacebook('update');
  			return false;
  		}
  	}
  	
	Sns.bannerShareToFacebook = function(img) {
		var that = this;
		if( that.isFbToken() ){
			$.ajax({
				type : "POST",
				url : that.base_url + "process/write.php?sns_type=facebook",
				dataType : "json",
				data : {
					img : img,
					fb_token : sessionStorage.getItem('fb_token'),
					msg : '',
					pic_msg : '아래 링크를 눌러서 확인해보세요.\n' +that.redirect_url+ '\n',
				},
				dataType : 'json',
				success : function(result) { 
					if(result.id){ alert('[FACEBOOK]공유성공'); }
					else{ alert('[FACEBOOK]공유실패');	 } 
				},
			});
		}else { return false; }
	}; 
	
	Sns.msgShareToFacebook = function(obj) {
		var that = this;
		if( that.isFbToken() ){
			var msg = obj.message,
			graph_url = that.fb_graph_url+"/me/feed?access_token=" + sessionStorage.getItem('fb_token'),
			store_name = ( obj.name==null )?"guppii스토어":obj.name,
			store_desc = ( obj.description==null )?"":obj.description,
			store_img = ( obj.store_img="" )?that.redirect_url+"images/default_store.png":obj.store_img;
			msg += "\n\n- "+store_name+"에 토크를 작성하셨습니다.";
			
			$.ajax({
				type : "POST",
				url : graph_url,
				data : {
					message : msg,
					name : store_name,
					caption : "Guppii",
					description : store_desc,
					picture : store_img,
					link : that.share_url+ obj.store_id,
				},
				dataType : 'json',
				success : function(result) {
					if (result.id) { alert('[FACEBOOK]공유성공'); } 
					else { alert('[FACEBOOK]공유실패'); }
					alert(INSERT_TALK_MESSAGE);
					location.reload();
				},
			});
		}else { return false; }
	}; 
	
	Sns.msgPhotoShareToFacebook = function(obj) {
		var that = this;
		if( that.isFbToken() ){
			$.ajax({
				type : "POST",
				url : Sns.base_url + "process/write.php?sns_type=facebook",
				dataType : "json",
				data : {
					img : obj.image,
					fb_token : sessionStorage.getItem('fb_token'),
					msg : obj.message,
					pic_msg : obj.name+"에 토크를 작성하셨습니다.",
				},
				dataType : 'json',
				success : function(result) { 
					if(result.id){ alert('[FACEBOOK]공유성공'); }
					else{ alert('[FACEBOOK]공유실패');	 } 
					alert(INSERT_TALK_MESSAGE);
					location.reload();
				}
			});
		}else { return false; }
	}; 
	
  	Sns.couponShareToFacebook = function(obj) {
  		var that = this;
  		if( that.isFbToken() ){
	  		var	coupon_desc=( obj.coupon_desc==null )?"":obj.coupon_desc,
			graph_url = that.fb_graph_url+"/me/feed?access_token=" + sessionStorage.getItem('fb_token'),
			store_name=( obj.store_name==null )?"guppii스토어":obj.store_name,
			coupon_title= obj.coupon_title.replace('쿠폰',''),
			msg = store_name+ "에서 발행한 "+coupon_title+"쿠폰을 공유하였어요.\n\n";
			msg += "아래 이미지를 클릭하여 쿠폰을 발급받아보세요. ^^\n";
			
			$.ajax({
				type : "POST",
				url : graph_url,
				data : {
					message : msg,
					name : store_name,
					caption : "Guppii",
					description : coupon_desc,
					picture : obj.coupon_img,
					link : Sns.share_url + obj.store_id,
				},
				dataType : 'json',
				success : function(result) {
					if (result.id) { alert('[FACEBOOK]공유성공');  } 
					else { alert('[FACEBOOK]공유실패'); }
				},
			});
		}else { return false; }
	}; 
})(window);
